/**
 * 分享窗口
 */
var ShareWindow = Window.extend({
    _ccsFileName: res.ShareWindow_json,
    initWindow: function (data) {
        var self = this;
        self.type = data.type;//分享类型
        self.shareType = data.shareType;//分享平台
        self.roomid = data.roomid || gameData.roomId;//房间号
        self.wanfaDesp = data.wanfaDesp;//玩法描述
        self.maxNum = data.maxNum;//最大人数
        self.cunNum = data.cunNum;//当前人数
        self.clubData = data.clubData;//俱乐部数据
        self.screenshot = data.screenshot;//截屏
        self.image = data.image;//图片
        self.playbackId = data.playbackId;//回放码
        self.gameDescribe = data.gameDescribe;//游戏邀请介绍
        self.uid1 = data.uid1;
        self.uid2 = data.uid2;
        self.fenshu = data.fenshu;
        self.ownerUid = data.ownerUid;
        if (self.shareType) {
            self.setVisible(false);
            self.setShareType(self.shareType);
        }
    },
    /**
     * 添加事件
     * @private
     */
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.liaobei': '_clickShareType',
            'root.weixinqian': '_clickShareType',
            'root.weixin': '_clickShareType',
            'root.xianliao': '_clickShareType',
            'root.dingding': '_clickShareType',
            'root.qq': '_clickShareType',
        };
    },
    /**
     * 添加点击事件
     * @private
     */
    _clickShareType: function (sender) {
        var self = this;
        switch (sender.getName()) {
            case ShareUtil.SharePlatForm.WeiXin:
                if (!WeChatUtil.isInstalled()) {
                    showAlert("您还没有安装微信");
                    return;
                }
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                if (!WeChatUtil.isInstalled()) {
                    showAlert("您还没有安装微信");
                    return;
                }
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                if (!XianLiaoUtil.isInstalled()) {
                    showAlert("您还没有安装闲聊");
                    return;
                }
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                if (!LiaoBeUtil.isInstalled()) {
                    showMessageBox('您还没有安装聊呗,是否前往下载？', function () {
                        cc.sys.openURL('https://www.liaobe.cn');
                    }, function () {
                    });
                    return;
                }
                break;
            case ShareUtil.SharePlatForm.DingDing:
                if (!DDUtil.isInstalled()) {
                    showAlert("您还没有安装钉钉");
                    return;
                }
                break;
        }
        self.setShareType(sender.getName());

    },
    /**
     * 区分分享类型
     * @param shareType
     */
    setShareType: function (shareType) {
        var self = this;
        self.removeFromParent(true);
        if (self.type == ShareUtil.ShareType.SCREENSHOT) {
            self.sharePic(shareType, self.screenshot);
        } else if (self.type == ShareUtil.ShareType.PLAYBACKID) {
            self.sharePlaybackId(shareType, self.playbackId)
        } else if (self.type == ShareUtil.ShareType.CLUB) {
            self.shareClub(shareType, self.clubData)
        } else if (self.type == ShareUtil.ShareType.ROOMID) {
            self.shareRoomId(shareType, self.roomid, self.wanfaDesp);
        } else if (self.type == ShareUtil.ShareType.GAME) {
            self.shareGame(shareType, self.gameDescribe);
        } else if (self.type == ShareUtil.ShareType.ZHANJI) {
            self.shareZhanJi(shareType, self.roomid, self.uid1, self.uid2, self.screenshot, self.fenshu, self.ownerUid);
        }
    },
    /**
     * 分享图片
     * @param platform
     * @param shareNode
     */
    sharePic: function (platform, shareNode) {
        switch (platform) {
            case ShareUtil.SharePlatForm.QQ:
                QQUtil.sharePic(shareNode);
                break;
            case ShareUtil.SharePlatForm.WeiXin:
                WeChatUtil.shareCapture(shareNode);
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                WeChatUtil.shareCapture(shareNode, WeChatUtil.SceneType.Round);
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                XianLiaoUtil.shareCapture(shareNode);
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                LiaoBeUtil.shareCapture(shareNode);
                break;
            case ShareUtil.SharePlatForm.DingDing:
                DDUtil.shareCapture(shareNode);
                break;
        }
    },
    /**
     * 分享回放码
     * @param platform
     * @param playbackId
     */
    sharePlaybackId: function (platform, playbackId) {
        var text = "玩家【" + gameData.nickname + "】分享了一个【" + app.appName + "】的回放码: " + playbackId + ", 在大厅点击进入战绩页面, 然后点击查看回放按钮, 输入回放码点击确定后即可查看.";
        var sceneType = 0;
        var transaction = getCurTimestamp() + gameData.uid;
        switch (platform) {
            case ShareUtil.SharePlatForm.QQ:
                QQUtil.shareText(text, sceneType, transaction);
                break;
            case ShareUtil.SharePlatForm.DingDing:
                DDUtil.shareText(text, sceneType, transaction);
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                XianLiaoUtil.shareText(text);
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                LiaoBeUtil.shareText(text, sceneType, transaction);
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                WeChatUtil.shareText(text, 1);
                break;
            case ShareUtil.SharePlatForm.WeiXin:
            default:
                WeChatUtil.shareText(text, 0);
                break;
        }
    },
    /**
     * 分享游戏
     * @param platform
     * @param gameDescribe
     */
    shareGame: function (platform, gameDescribe) {
        var title = "【" + app.appName + "】";
        var description = gameDescribe;
        switch (platform) {
            case ShareUtil.SharePlatForm.QQ:
                QQUtil.shareUrl(app.shareDownLoadUrl, title, description, 0, getCurTimestamp() + gameData.uid);
                break;
            case ShareUtil.SharePlatForm.DingDing:
                DDUtil.shareUrl(app.shareDownLoadUrl, title, description);
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                LiaoBeUtil.shareUrl(app.shareDownLoadUrl, title, description);
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                XianLiaoUtil.shareGame("0", title, description);
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                WeChatUtil.shareUrl(app.shareDownLoadUrl, description, title, 1);
                break;
            case ShareUtil.SharePlatForm.WeiXin:
            default:
                WeChatUtil.shareUrl(app.shareDownLoadUrl, title, description, 0);
                break;
        }
    },
    /**
     * 分享房间ID
     * @param platform
     * @param roomId
     * @param wanfaDesp
     */
    shareRoomId: function (platform, roomId, wanfaDesp) {
        var self = this;
        var wanfa = self.handleWanfaDesp(wanfaDesp ? wanfaDesp : gameData.wanfaDesp);
        var id = roomId ? roomId : gameData.roomId;
        var url = app.shareUrl + '?roomid=' + id;
        var maxNum = self.maxNum || gameData.playerNum;
        var cunNum = self.cunNum || (gameData.players ? gameData.players.length : self.cunNum);
        var parts = decodeURIComponent(wanfa).split(',');
        var mapName = parts[0];
        parts.splice(0, 1);
        var title = app.companyName + "-" + mapName + '-' + id + (maxNum ? ",已有(" + cunNum + "/" + maxNum + ")人" : ",已有" + cunNum + "人");
        var description = "房号: " + id + "," + (parts.length ? parts.join(',') + ',' : "") + "速度来啊! 【" + app.appName + "】";

        var MWSign = id ^ gameData.ownerUid;
        var MWArea = app.area;
        url = url + "&area=" + MWArea + "&sign=" + MWSign + "&details=true" + "#";

        //url = app.shareDownLoadUrl;
        switch (platform) {
            case ShareUtil.SharePlatForm.QQ:
                QQUtil.shareUrl(url, title, description, 0, getCurTimestamp() + gameData.uid);
                break;
            case ShareUtil.SharePlatForm.DingDing:
                DDUtil.shareUrl(url, title, description);
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                LiaoBeUtil.shareUrl(url, title, description);
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                XianLiaoUtil.shareGame(roomId, title, description);
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                WeChatUtil.shareUrl(url, description, title, 1);
                break;
            case ShareUtil.SharePlatForm.XiaoChengXu:
                var area = app.area;
                var sign = gameData.uid ^ roomId;
                var path = "/pages/index/index?" + "&area=" + area + "&appId=" + app.appId + "&roomid=" + roomId + "&sign=" + sign;
                WeChatUtil.shareMiniPro(app.shareDownLoadUrl, title, description, "res/icon.png", app.miniId, path);
                break;
            case ShareUtil.SharePlatForm.WeiXin:
            default:
                WeChatUtil.shareUrl(url, title, description, 0);
                break;
        }
    },
    /**
     * 分享俱乐部
     * @param platform
     * @param clubData
     */
    shareClub: function (platform, clubData) {
        var title = app.appName + "-俱乐部";
        var description = "玩家[" + gameData.nickname + "]邀请您加入俱乐部[" + clubData['name'] + "]，俱乐部ID[" + clubData['_id'] + "]" + "速度来啊! 【" + app.appName + "】";
        var clubID = clubData['_id'].toString();
        var strID = "";
        for (var i = clubID.length; i < 6; i++) {
            strID = strID + "0";
        }
        strID = strID + clubID;
        var url =  app.shareClubUrl + '?roomid=' + strID;
        if (gameData.link == 2) {
            url = url
                + "&inviterHead=" + gameData.headimgurl
                + "&club=" + encodeURIComponent(clubData['name'])
                + '&inviterName=' + encodeURIComponent(gameData.nickname)
                + '&creatorHead=' + clubData['head']
                + '&clubNum=' + clubData['players_count']
                + '&creatorName=' + encodeURIComponent(clubData['nick']);
        }
        url = app.shareDownLoadUrl;
        switch (platform) {
            case ShareUtil.SharePlatForm.QQ:
                QQUtil.shareUrl(url, title, description, 0, getCurTimestamp() + gameData.uid);
                break;
            case ShareUtil.SharePlatForm.DingDing:
                DDUtil.shareUrl(url, title, description, 0);
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                LiaoBeUtil.shareUrl(url, title, description, 0);
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                XianLiaoUtil.shareGame(strID, title, description);
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                WeChatUtil.shareUrl(url, title, description, 1);
                break;
            case ShareUtil.SharePlatForm.WeiXin:
            default:
                WeChatUtil.shareUrl(url, title, description, 0);
                break;
        }
    },
    /**
     * 分享战绩
     * @param platform
     * @param roomId
     * @param wanfaDesp
     */
    shareZhanJi: function (platform, roomId, uid1, uid2, shareNode, fenshu, ownerUid) {
        var self = this;
        var sign = Crypto.MD5("fy-game-result" + app.area + roomId + uid1 + uid2);
        var url = "https://pay.yayayouxi.com/zhanji/index" + '?area=' + app.area + '&roomId=' + roomId + '&uid1=' + uid1 + '&uid2=' + uid2 + '&sign=' + sign;
        if (ownerUid != undefined) {
            url = "https://pay.yayayouxi.com/zhanji/index" + '?area=' + app.area + '&roomId=' + roomId + '&uid1=' + uid1 + '&uid2=' + uid2 + '&sign=' + sign + '&ownerUid=' + ownerUid;
        }
        switch (platform) {
            case ShareUtil.SharePlatForm.QQ:
                QQUtil.sharePic(shareNode);
                break;
            case ShareUtil.SharePlatForm.DingDing:
                DDUtil.shareCapture(shareNode);
                break;
            case ShareUtil.SharePlatForm.LiaoBai:
                LiaoBeUtil.shareCapture(shareNode);
                break;
            case ShareUtil.SharePlatForm.XianLiao:
                XianLiaoUtil.shareCapture(shareNode);
                break;
            case ShareUtil.SharePlatForm.WeiXinQuan:
                var path = cc.path.join(cc.sys.writablePath, 'res/01.png');
                if(!jsb.fileUtils.isFileExist(path)){
                    path = "res/01.png";
                }
                if (jsb.fileUtils.isFileExist(path) && window.nativeVersion >= "6.0.0") {
                    WeChatUtil.shareUrlWithIcon(url, '战绩 房间号:' + roomId, self.fenshu, path, 1);
                }
                else {
                    WeChatUtil.shareUrl(url, '战绩 房间号:' + roomId, self.fenshu, 1);

                }
                break;
            case ShareUtil.SharePlatForm.WeiXin:
            default:
                var path = cc.path.join(cc.sys.writablePath, 'res/01.png');
                if(!jsb.fileUtils.isFileExist(path)){
                    path = "res/01.png";
                }
                if (jsb.fileUtils.isFileExist(path) && window.nativeVersion >= "6.0.0") {
                    WeChatUtil.shareUrlWithIcon(url, '战绩 房间号:' + roomId, self.fenshu, path, 0);
                }
                else {
                    WeChatUtil.shareUrl(url, '战绩 房间号:' + roomId, self.fenshu, 0);
                }
                break;
        }
    },
    handleWanfaDesp: function (wanfaDesp){
        var removedStrArr =['群主支付', '房主支付', 'AA支付'];
        var retVal = wanfaDesp;
        var retValArr = retVal.split(',');
        var locationArr = [];
        for (var i = 0; i < retValArr.length; i++){
            retValArr[i] = retValArr[i].trim();
            if (removedStrArr.indexOf(retValArr[i]) > -1) locationArr.push(i);
        }

        for (var i = locationArr.length - 1; i > -1; i--){
            retValArr.splice(locationArr[i], 1);
        }

        retVal = retValArr.join(',');

        return retVal;
    },
});